<?php
/*
 * 商品管理
 */
namespace Admin\Controller;
use Admin\Model\goodsModel;
use Admin\Model\goodstypeModel;
use Admin\Model\goodsguigeModel;
use Admin\Model\memberModel;
use Common\Service\ShareService;
use Think\Controller;
use Think\Page;

class ShareController extends BaseController {

    
    public function __construct() {
        parent::__construct();
        self::getType();
    }

    public function getType(){
        $typeinfo=ShareService::S()->shareType();
        $newType=array();
        foreach ($typeinfo as $k=>$v){
            $newType[$v['type_id']]=$v;
        }
        //dump($newType);die;
        $this->assign('gtype',$newType);
    }
    protected function _get_goods_where(){
        $arrow = array(
            "share_name","ischeck","type_id"
        );//允许提交的数据
        $data = param_filter($_GET,$arrow,$must=NULL,$filter=true);//过滤数据

        $where = array();
        //todo
        if(is_set($data,'share_name')){
            $where['share_name']=$data['goods_name'];
            //$where .= " AND goods_name like '%".$data['goods_name']."%'";
        }

        if(is_set($data,'type_id')){
            //$where .= " AND gtype_id =".$data['gtype_id'];
            $where['share_type']=$data['type_id'];
        }

        return $where;
    }

    /*
     * 统计商品数量
     */
    protected function _get_goods_count(){
        $where = $this->_get_goods_where();//不带status的查询条件
        //全部订单
        //0全部,1展示中,2下架
        $goods_count =  array();
        $goods_count[0] = ShareService::S()->getShareCount($where);//$this->goodsModel->getGoodsCount($where);//
        $where['ischeck']=1;
        $goods_count[1] = ShareService::S()->getShareCount($where);
        $where['ischeck']=0;
        $goods_count[2] = ShareService::S()->getShareCount($where);//
        return $goods_count;
    }
    
    /*
     * 删除
     */
    public function del(){

        $id = get("id");
        $res = ShareService::S()->delete($id);//$this->goodsModel->where($where)->delete();
        if($res){
            return_json(0,"删除成功");
        }
        return_json(1,"删除失败");
    
    }

    /*
     * 商品列表
     */
    public function index(){
        $page = get('p',1);
        $where = $this->_get_goods_where();
        if(is_set($_GET,'ischeck')){
            //$where .= " AND ischeck = '".get('ischeck')."'";
            $where['ischeck']=get('ischeck');
        }

        //统计数量
        $goods_count = $this->_get_goods_count();
        $this->assign('goods_count',$goods_count);

        $goods_list =ShareService::S()->queryListInfo($where,$page);// $this->goodsModel->getGoodsList($where,$page);
        $this->assign('datalist',$goods_list);
        //dump($goods_list);die;
        $this->display();
    }
    
    
    /*
     * 出售中的的商品
     */
    public function onsale(){
        $page = get('p',1);
        $where = $this->_get_goods_where();
        $where['ischeck']=1;//.= " AND ischeck = 1";
    
        //统计数量
        $goods_count = $this->_get_goods_count();
        $this->assign('goods_count',$goods_count);

        $goods_list = ShareService::S()->queryListInfo($where,$page);
        $this->assign('datalist',$goods_list);
        $this->display('index');
    }
    
    /*
     * 仓库中的的商品
     */
    public function outsale(){
        $page = get('p',1);
        $where = $this->_get_goods_where();
        $where['ischeck']=0;// .= " AND ischeck = 0";

        //统计数量
        $goods_count = $this->_get_goods_count();
        $this->assign('goods_count',$goods_count);
        
        $goods_list = ShareService::S()->queryListInfo($where,$page);
        $this->assign('datalist',$goods_list);
        $this->display('index');
    }

    
    /*
     * 添加商品图片
     */
    public function uploadpic(){
        
        if(empty($_FILES)){
            return_json(1,"请上传图片");
        }
        
        $res = $this->iupload("goods");//上传操作
        //上传失败
        if(empty($res)){
            return_json(1,"上传失败,请重新上传");
        }
        
        return_json(0,"上传完毕",$res);
    }

    public function addShare(){
        $share_id=I('get.share_id');
        $data['share_title']=I('post.share_title');
        $data['share_type']=I('post.share_type');
        $data['share_data']=I('post.share_data');
        $data['share_text']=I('post.share_text');
        $data['video_thumb']=I('post.video_thumb');
        $data['ord']=I('post.ord');
        $data['video_thumb']=json_encode(explode("<br>",$data['video_thumb']));
        $goods_thumb=I('post.goods_thumb');
        $data['share_pic']=$goods_thumb[0];
        $data['share_thumb']=json_encode($goods_thumb);
        $data['ischeck']=I('post.ischeck');
        //dump($data);die;
        if(empty($data['share_title'])){
            return_json(1,"分享标题不能为空");
        }if(empty($data['share_type'])){
            return_json(1,"分享类型不能为空");
        }if(empty($data['share_text'])){
            return_json(1,"分享内容不能为空");
        }
        $shareInfo=ShareService::S()->findDetail(array('id'=>$share_id));
        if($shareInfo){
            $where['id']=$share_id;
            $re=ShareService::S()->saveInfo($where,$data);
            if($re===false){
                return_json(1,"修改失败");
            }else{
                return_json(0,"修改成功");
            }
        }else{
            $data['create_time'] = date('Y-m-d H:i:s');
            $re=ShareService::S()->addInfo($data);
            if($re){
                return_json(0,"新增成功",'','/admin/share');
            }else{
                return_json(1,"新增失败");
            }
        }
        //dump($data);die;
    }


    
    /*
     * 编辑商品
     */
    public function edit(){
        $goods_id = get("share_id");
        $where  = array(
            "id" => $goods_id
        );
        $goods =ShareService::S()->findDetail($where) ;//$this->goodsModel->getGoods($where);
        $video=json_decode($goods['video_thumb'],true);
        $goods['video_thumb']='';
        foreach ($video as $Vvideo){
            $goods['video_thumb'].=$Vvideo;
        }
        if(empty($goods_id)){
            $this->error("该资源不存在","/admin/share/index");exit;
        }
        $this->assign("goods",$goods);

        $this->display();
    }
    
    
    /*
     * 编辑详情
     */
    public function editdetail(){
        
        $goods_id = get("goods_id");
        $where  = array(
            "goods_id" => $goods_id
        );
        
        if(IS_AJAX){

            $allow = $must = array("goods_desc");
            $data = param_filter($_POST, $allow,$must,FALSE);
            if(empty($goods_id)){
                return_json(1,"商品参数错误");
            }
            if(empty($data)){
                return_json(1,"商品详情不能为空");
            }

            $res = $this->goodsModel->editGoods($where, $data);
            if($res !== false){
                return_json(0,"编辑商品详情成功");
            }
            return_json(1,"编辑商品出错");
        }
        

        $goods = $this->goodsModel->getGoods($where);
        if(empty($goods_id)){
            $this->error("该商品不存在","/admin/goods/index");exit;
        }
        $goods['goods_desc'] = stripslashes($goods['goods_desc']);
        $this->assign("goods",$goods);

        $this->display();
    }
    

    
    


    /*
     * 商品分类
     */
    public function fenlei(){
        //$fenlei = $this->_get_gtype();
        $this->display();
    }
    
    /*
     * 编辑分类
     */
    public function edittype(){
        $id=I('get.id');
        if($_POST){
            //dump($_POST);die;
            $saveId=I('post.type_id');
            $save['ord']=I('post.ord');
            $save['type_name']=I('post.type_name');
            if(!empty($saveId)){
                $where['type_id']=$saveId;
                $re=ShareService::S()->saveShareType($where,$save);
                if($re===false){
                    $this->error2('修改失败');
                }
            }else{
                $re=ShareService::S()->addShareType($save);
                if(!$re){
                    $this->error2('添加分类失败');
                }
            }
            //$this->success2('操作成功','fenlei');
            //dump($_POST);die;
            //$this->display('fenlei');
            $this->redirect('fenlei');
        }else{
            $typeinfo=ShareService::S()->shareType(array('type_id'=>$id));
            //dump($typeinfo[0]);die;
            $this->assign('data',$typeinfo[0]);
            $this->display();
        }
    }

    public function add(){

        $this->display('edit');
    }
    
    public function deltype(){
        $id = get("id");
        if($id){
            $res =ShareService::S()->delShareType($id);//$this->goodstypeModel->where(array("type_id"=>$id))->delete();
            if($res){
                return_json(0,"删除成功","","/admin/share/fenlei");
            }
        }
        return_json(1,"删除失败");
    }
    
       
    /*
     * 供应商编辑页面
     */
    /*
     * 商品列表
     */
    public function supplier(){
        $page = get('p',1);
        $where = $this->_get_goods_where();
        if(is_set($_GET,'ischeck')){
            $where .= " AND ischeck = '".get('ischeck')."'";
        }
        //统计数量
        $goods_count = $this->_get_goods_count();
        $this->assign('goods_count',$goods_count);

        $goods_list = $this->goodsModel->getGoodsList($where,$page);
        $this->assign('datalist',$goods_list);
        $this->display();
    }
    /*
     * 编辑商品
     */
    public function supplieredit(){
        $goods_id = get("goods_id");
        $where  = array(
            "goods_id" => $goods_id
        );
        $goods = $this->goodsModel->getGoods($where);
        if(empty($goods_id)){
            $this->error("该商品不存在","/admin/goods/index");exit;
        }
    
        //获取商品规格
        $guige = $this->goodsguigeModel->getGoodsguigeLists($where);
        $goods['guige'] = $guige;
    
        //jinjianghai  test
        $guige_new = unserialize($goods['guige_new']);
        $this->assign('guige_new',$guige_new);
    
        $this->assign("goods",$goods);
        $this->display();
    }
    
    
    
}